<template>
  <div>
    <Form ref="formInline" :model="map" inline>
      <div class="search">
        <Button type="primary" class="fl" icon="plus" @click="add">增加角色</Button>
        <Button type="primary" class="fl" icon="plus" @click="listResource" style="margin-left: 20px;">资源管理</Button>
        <!--<FormItem>-->
          <!--<Button type="primary" class="fl" icon="plus" @click="add">增加角色</Button>-->
        <!--</FormItem>-->
        <!--<FormItem>-->
          <!--<Button type="primary" class="fl" icon="plus" @click="listResource">资源管理</Button>-->
        <!--</FormItem>-->

        <FormItem>
          <Input type="text" v-model="map.key" placeholder="关键字"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="getList">查询</Button>
        </FormItem>
      </div>
    </Form>
    <Table border stripe :loading="loading" :columns="columns" :data="data && data.data"></Table>
    <Page class="page" :total="data && data.count" :page-size="data && data.pagesize" show-total
          @on-change="changePage"></Page>

  </div>
</template>
<script>
  import {role, user} from "@/api";
  import {Button, Form, FormItem, Input, Page, Table} from 'iview';

  export default {
    components: {
      Button,
      Table,
      Page,
      Form,
      FormItem,
      Input
    },
    data() {
      return {
        loading: true,
        columns: [
          {
            title: "角色",
            key: "name"
          },
          {
            title: "角色名称",
            key: "title"
          },
          {
            title: "可删除",
            key: "deletable",
            align: "center",
            //sortable: true,

            render: (h, params) => {
              return h('span', params.row.deletable ? '允许' : '不允许');
            }
          },
          {
            title: "描述",
            key: "description"
          },
          {
            title: "创建时间",
            key: "create_time",
            width: 200,
            align: "center",
            render: (h, params) => {
              if(!params.row.create_time) return "";
              return h('span', new Date(params.row.create_time * 1000).toLocaleString());
            }
          },
          {
            title: "操作",
            key: "action",
            width: 400,
            align: "left",
            render: (h, params) => {

              let ops = [
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      // icon:'ios-list'
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.$router.push({
                          path: "/ucenter/role/save",
                          query: {
                            id: params.row.id
                          }
                        });
                      }
                    }
                  },
                  '编辑'
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      // icon:'ios-list'
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.$router.push({
                          path: "/ucenter/user/list",
                          query: {
                            role_id: params.row.id
                          }
                        });
                      }
                    }
                  },
                  '用户'
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      // icon:'ios-list'
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.$router.push({
                          path: "/ucenter/auth/list",
                          query: {
                            role_id: params.row.id
                          }
                        });
                      }
                    }
                  },
                  '权限'
                ),
              ];
              if(params.row.deletable) {
                ops.push(h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                      // icon:'trash-a'
                    },
                    on: {
                      click: () => {
                        if(confirm("确定要删除吗？")) {
                          this.delete(params.row.id, params.index);
                        }
                      }
                    },

                  },
                  '删除'
                ))
              }

              return h("div", ops);

            }
          }
        ],
        data: {},
        map: {
          page: 1,
          key: "",
          pageSize: 10,
        }
      };
    },
    methods: {
      getList() {
        this.loading = true;
        role.getList(this.map).then(res => {
          this.data = res.data;
          this.loading = false;
        });
      },
      delete(id, index) {
        role.delete(id).then(res => {
          // this.data.splice(index, 1);
          this.getList();
        });
      },
      changePage(index) {
        this.map.page = index;
        this.getList();
      },
      add() {
        this.$router.push('/ucenter/role/save');
      },
      listResource() {
        this.$router.push({
          path: "/ucenter/resource/list",
          query: {
            //role_id: params.row.id
          }
        });
      }
    },
    mounted() {
      this.getList();
    }
  };
</script>
